package com.cc.bsp.action.common.query;

import java.util.List;

import com.cc.bsp.action.IQueryAction;
import com.cc.bsp.dto.IDtoEnitity;
import com.cc.bsp.exception.ActionWebException;
import com.cc.bsp.exception.ServiceException;
import com.cc.bsp.persistence.dao.IQueryPageOrder;
import com.cc.bsp.service.IRootService;
import com.opensymphony.xwork2.ActionSupport;

public abstract class CommonServiceQueryAction<ID extends java.io.Serializable>
		extends ActionSupport implements IQueryAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7905995289948563759L;

	private IDtoEnitity dto;

	private List<IDtoEnitity> dtos;

	private ID id;

	private IQueryPageOrder pageOrder;

	public abstract IRootService getRootService();

	public String findById() throws ActionWebException {

		try {
			dto = getRootService().findById(id);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ActionWebException(e);
		}

		return ActionSupport.SUCCESS;
	}

	public String findOne() throws ActionWebException {

		try {
			dto = getRootService().findOne(dto);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ActionWebException(e);
		}

		return ActionSupport.SUCCESS;
	}

	public String findList() throws ActionWebException {

		try {
			if (pageOrder == null) {
				dtos = getRootService().findList(dto);
			} else {
				dtos = getRootService().findList(dto, pageOrder);
			}

		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ActionWebException(e);
		}

		return ActionSupport.SUCCESS;
	}

	public String findAll() throws ActionWebException {

		try {
			if (pageOrder == null) {
				dtos = getRootService().findAll(dto);
			} else {
				dtos = getRootService().findAll(dto, pageOrder);
			}

		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ActionWebException(e);
		}

		return ActionSupport.SUCCESS;
	}
	

	public String execute() throws Exception {

		return super.execute();
	}

	public IDtoEnitity getDto() {
		return dto;
	}

	public void setDto(IDtoEnitity dto) {
		this.dto = dto;
	}

	public List<IDtoEnitity> getDtos() {
		return dtos;
	}

	public void setDtos(List<IDtoEnitity> dtos) {
		this.dtos = dtos;
	}

	public ID getId() {
		return id;
	}

	public void setId(ID id) {
		this.id = id;
	}

	public IQueryPageOrder getPageOrder() {
		return pageOrder;
	}

	public void setPageOrder(IQueryPageOrder pageOrder) {
		this.pageOrder = pageOrder;
	}

}
